#include "dlg_adduser.h"
#include "ui_dlg_adduser.h"
#include "supsql.h"
#include<QMessageBox>
#include <QDebug>

dlg_adduser::dlg_adduser(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dlg_adduser)
{
    ui->setupUi(this);
}

dlg_adduser::~dlg_adduser()
{
    delete ui;
}

void dlg_adduser::setwType(bool isAdd, UserInfo info)
{
    w_isAdd = isAdd;
    w_info = info;
    ui->EditwName->setText(info.name);
    ui->Editwuser->setText(QString::number((info.username)));
    ui->Editwaddr->setText(info.address);
    ui->Editwphone->setText(info.phone);
    ui->EditwAur->currentText();

}

void dlg_adduser::on_btnExit_clicked()
{
    exit(0);
}

void dlg_adduser::on_btnOk_clicked()
{
    UserInfo info;
    info.name=ui->EditwName->text();
    info.username= ui->Editwuser->text().toInt();
    info.address= ui->Editwaddr->text();
    info.phone= ui->Editwphone->text();
    info.Aur = ui->EditwAur->currentText();
    auto ptr = supsql::getinstance();
    if(w_isAdd)
    {
        ptr->AddUser(info);
    }
    else
    {
        w_info.name=ui->EditwName->text();
        w_info.username= ui->Editwuser->text().toInt();
        w_info.address= ui->Editwaddr->text();
        w_info.phone= ui->Editwphone->text();
        w_info.Aur = ui->EditwAur->currentText();
        ptr->changeUserInfo(w_info);
    }
    QMessageBox::information(nullptr,"信息","存储成功");
    this->hide();
}

